import inspect
from shutil import rmtree

from encrypt.xBogus import XBogus
from manager.recorder import DownloadRecorder
from src.application.server_api.serverApi import ServerApi
from src.config.parameter import Parameter
from src.config.settings import Settings
from src.custom.internal import PROJECT_ROOT
from src.module.cookie import Cookie


class Core:
    def __init__(self):
        self.parameter = None
        self.x_bogus = XBogus()
        self.cookie = Cookie()
        self.settings = Settings(PROJECT_ROOT)
        self.blacklist = None

    def check_config(self):
        self.blacklist = DownloadRecorder(
            True,
            PROJECT_ROOT.joinpath("./cache"),
            False)

    def check_settings(self):
        # 获取Parameter类的__init__方法所需的参数列表
        expected_params = inspect.signature(Parameter.__init__).parameters.keys()

        # 过滤掉不需要的参数
        filtered_params = {k: v for k, v in self.settings.read().items() if k in expected_params}
        self.parameter = Parameter(settings=self.settings, cookie_object=Cookie(), main_path=PROJECT_ROOT, xb=self.x_bogus, **filtered_params, blacklist=self.blacklist)
        # self.parameter.cookie_object.run("xgplayer_user_id=937514098952; bd_ticket_guard_client_web_domain=2; FORCE_LOGIN=%7B%22videoConsumedRemainSeconds%22%3A180%7D; volume_info=%7B%22isUserMute%22%3Afalse%2C%22isMute%22%3Atrue%2C%22volume%22%3A0.5%7D; passport_csrf_token=8d10796d6bf8b339efbb598bcf1277bb; passport_csrf_token_default=8d10796d6bf8b339efbb598bcf1277bb; _tea_utm_cache_1243=undefined; MONITOR_WEB_ID=027c1eaf-01ab-4f6b-8e5c-df0e2bc4bc4c; strategyABtestKey=%221712818170.557%22; download_guide=%223%2F20240411%2F0%22; pwa2=%220%7C0%7C3%7C0%22; __live_version__=%221.1.1.9558%22; live_can_add_dy_2_desktop=%220%22; live_use_vvc=%22false%22; tt_scid=Z-anqfZYzlaTnunGQQcIwECYHDBMqchhydlvhOLRgcNoJVj-9b99-K-vkL.fvZhR160e; bd_ticket_guard_client_data=eyJiZC10aWNrZXQtZ3VhcmQtdmVyc2lvbiI6MiwiYmQtdGlja2V0LWd1YXJkLWl0ZXJhdGlvbi12ZXJzaW9uIjoxLCJiZC10aWNrZXQtZ3VhcmQtcmVlLXB1YmxpYy1rZXkiOiJCTWwweG9tTHdWZnRuUktDT1ZIWkFaYjZyNGdyakdNYjVScWZ1NWU2blVZc2pLQVQvdHhVSGJSUVdzUEd4UzY2NTZSMy9pZUlaczhOU01YRU9HeThwV1U9IiwiYmQtdGlja2V0LWd1YXJkLXdlYi12ZXJzaW9uIjoxfQ%3D%3D; msToken=VSlZ7wc6kSBAw7b72AJaIU93iZSsL5-V7w5qSijHl3MWBkFHzjq4e1Q8_M_r3bqyQ0TQtqkAFD1RdxhnthZ2Z4WJZUm86Qf72E-yxOPtirhCH4d1aGhhLktkWGS0DmU=; douyin.com; xg_device_score=7.802204888412783; device_web_cpu_core=12; device_web_memory_size=8; architecture=amd64; csrf_session_id=b9f75145480b2fd5d92dc79821f952a5; msToken=r2x9AF3dAqUa8qQxzE5ZOvGsnFl2Inl_7xo--LOeFe5aZwP5h_qCMi6gKE43i60kgTUeCXZ2ooLLhrhIg3FQVT9PO_UqqUJbOG2FnckUb86qD2JjpPALb4je7S8V4Bo=; __ac_nonce=06617ff04000cfd0eebcf; __ac_signature=_02B4Z6wo00f01GjNGaQAAIDDVWQDOD1DdsBo7R0AAHwsaacuJleKcU25IVoPE5nq5sHe2CXLiwcignFOM.vKfUtpliOo5kdXhHfGgL5jloMhd41HljdiUkqaAqJB7QSK7bgbagpTa4LpnTRc61; IsDouyinActive=true; home_can_add_dy_2_desktop=%220%22; dy_swidth=1536; dy_sheight=960; stream_recommend_feed_params=%22%7B%5C%22cookie_enabled%5C%22%3Atrue%2C%5C%22screen_width%5C%22%3A1536%2C%5C%22screen_height%5C%22%3A960%2C%5C%22browser_online%5C%22%3Atrue%2C%5C%22cpu_core_num%5C%22%3A12%2C%5C%22device_memory%5C%22%3A8%2C%5C%22downlink%5C%22%3A10%2C%5C%22effective_type%5C%22%3A%5C%224g%5C%22%2C%5C%22round_trip_time%5C%22%3A100%7D%22; stream_player_status_params=%22%7B%5C%22is_auto_play%5C%22%3A0%2C%5C%22is_full_screen%5C%22%3A0%2C%5C%22is_full_webscreen%5C%22%3A0%2C%5C%22is_mute%5C%22%3A1%2C%5C%22is_speed%5C%22%3A1%2C%5C%22is_visible%5C%22%3A1%7D%22")
        # self.parameter.update_cookie()

    def run(self):
        self.check_config()
        self.check_settings()
        self.serve_api()

    def close(self):
        self.delete_temp()
        print("程序结束运行")

    def delete_temp(self):
        rmtree(self.parameter.temp.resolve())

    def serve_api(self):
        app = ServerApi(self.parameter).run_server()
        app.run(host="0.0.0.0", port=self.parameter.settings.get('app_port', 5001))
